unit uCad_TabelaDesconto;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Dialogs, Grids, DBGrids, Mask,
  Formulario_Padrao, StdCtrls, RDprint, ExtCtrls, ComCtrls, TFlatButtonUnit, Forms, Buttons, DB,
  DBClient, ToolEdit, CurrEdit;

type
  TfrmCad_TabDesc = class(Tfrm_padr)
    lblCodEntidade: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    CDS: TClientDataSet;
    grpDesc: TGroupBox;
    CDSDesc1: TFloatField;
    CDSDesc2: TFloatField;
    CDSDesc3: TFloatField;
    CDSDesc4: TFloatField;
    CDSDesc5: TFloatField;
    CDSDesc6: TFloatField;
    CDSDesc7: TFloatField;
    CDSDesc8: TFloatField;
    CDSDesc9: TFloatField;
    CDSDesc10: TFloatField;
    dbg: TDBGrid;
    DS: TDataSource;
    edValor: TCurrencyEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure dbgDblClick(Sender: TObject);
    procedure dbgCellClick(Column: TColumn);
    procedure edValorKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnCancelarClick(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure BtnEliminarClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Cod_Entidade: String;
    Ent_Nome    : String;
  end;

var
  frmCad_TabDesc: TfrmCad_TabDesc;

implementation

uses
  clsTABELADESCONTO;

var
 Tabela: TTABELADESCONTO;
 Coluna: String;

{$R *.dfm}

procedure TfrmCad_TabDesc.FormCreate(Sender: TObject);
begin
  inherited;
 Tabela:= TTABELADESCONTO.Create;
end;

procedure TfrmCad_TabDesc.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
 Tabela.Free;
end;

procedure TfrmCad_TabDesc.FormActivate(Sender: TObject);
begin
  inherited;
 Panel1.Enabled:= True;
 Panel2.Enabled:= True;
 CDS.Close;
 CDS.CreateDataSet;
 CDS.Open;
 Tabela.Recuperar(Cod_Entidade);
 CDS.Append;
 CDSDesc1.AsFloat := Tabela.TAB_DESC1;
 CDSDesc2.AsFloat := Tabela.TAB_DESC2;
 CDSDesc3.AsFloat := Tabela.TAB_DESC3;
 CDSDesc4.AsFloat := Tabela.TAB_DESC4;
 CDSDesc5.AsFloat := Tabela.TAB_DESC5;
 CDSDesc6.AsFloat := Tabela.TAB_DESC6;
 CDSDesc7.AsFloat := Tabela.TAB_DESC7;
 CDSDesc8.AsFloat := Tabela.TAB_DESC8;
 CDSDesc9.AsFloat := Tabela.TAB_DESC9;
 CDSDesc10.AsFloat:= Tabela.TAB_DESC10;

 lblCodEntidade.Caption:= Cod_Entidade;
 Label1.Caption        := Ent_Nome;
 
 CDS.Post;
 dbg.SetFocus;
 edValor.Value:= 0;    
end;

procedure TfrmCad_TabDesc.dbgDblClick(Sender: TObject);
begin
  inherited;
 edValor.Value:= CDS.fieldbyname(Coluna).AsFloat;
 edValor.SetFocus;
 grpDesc.Caption:= Coluna;
end;

procedure TfrmCad_TabDesc.dbgCellClick(Column: TColumn);
begin
  inherited;
 Coluna:= Column.FieldName;
end;

procedure TfrmCad_TabDesc.edValorKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
 if key = 12 then begin
  CDS.Edit;
  CDS.FieldByName(Coluna).AsFloat:= edValor.Value;
  CDS.Post;
  dbg.SetFocus;
  edValor.Value:= 0;
  grpDesc.Caption:='';
 end;
end;

procedure TfrmCad_TabDesc.btnCancelarClick(Sender: TObject);
begin
  inherited;
 FormActivate(sender);
end;

procedure TfrmCad_TabDesc.btnGravarClick(Sender: TObject);
begin
  inherited;
 Tabela.TAB_DESC1:= CDSDesc1.AsFloat;
 Tabela.TAB_DESC2:= CDSDesc2.AsFloat;
 Tabela.TAB_DESC3:= CDSDesc3.AsFloat;
 Tabela.TAB_DESC4:= CDSDesc4.AsFloat;
 Tabela.TAB_DESC5:= CDSDesc5.AsFloat;
 Tabela.TAB_DESC6:= CDSDesc6.AsFloat;
 Tabela.TAB_DESC7:= CDSDesc7.AsFloat;
 Tabela.TAB_DESC8:= CDSDesc8.AsFloat;
 Tabela.TAB_DESC9:= CDSDesc9.AsFloat;
 Tabela.TAB_DESC10:= CDSDesc10.AsFloat;
 Tabela.Gravar(Cod_Entidade);
 Close;
end;

procedure TfrmCad_TabDesc.BtnEliminarClick(Sender: TObject);
begin
  inherited;
 if Application.MessageBox('Deseja eliminar tabela de descontos?','Confirmar',mb_YesNo+mb_IconQuestion+mb_DefButton2+mb_applmodal) = idyes then
  Tabela.Excluir(Cod_Entidade);
end;

end.
